#include "json_controller.h"
#include "cJSON.h"
#include <exception>
#include <stdexcept>

JSONController* JSONController::jcInstance=0;

JSONController::JSONController():workingDirectory(_T("")),nameCon(0)
{

}

JSONController::JSONController(const wxString& directory)
{
	workingDirectory=directory;
}

JSONController* JSONController::getInstance()
{
	if(jcInstance==0)
	{
		jcInstance=new JSONController();
	}
	return jcInstance;
}

JSONController* JSONController::getInstance(const wxString& dir)
{
	if(jcInstance==0)
	{
		jcInstance=new JSONController(dir);
	}
	else
	{
		jcInstance->setWorkingDirectory(dir);
	}
	return jcInstance;
}

JSONController::~JSONController()
{
	if(nameCon!=0)
	{
		delete nameCon;
		nameCon=0;
	}
	if(JSONController::jcInstance!=0)
	{
		delete JSONController::jcInstance;
		JSONController::jcInstance=0;
	}
}

void JSONController::setWorkingDirectory(const wxString& dir)
{
	workingDirectory=dir;
}

void JSONController::loadDirectory()
{
	if(workingDirectory.Length()>0)
	{
		if(nameCon==0)
		{
			nameCon=new NameController();
		}
		nameCon->load(workingDirectory);
	}
	else
	{
		throw std::runtime_error("Raw Directory does NOT set.");
	}
}

void JSONController::loadDirectory(const wxString& directory)
{
	workingDirectory=directory;
	loadDirectory();
}

NameController* JSONController::getNameController()
{
	if(nameCon==0)
	{
		nameCon=new NameController();
	}
	return nameCon;
}